package com.xxgc.spring.controller;

import com.xxgc.spring.bean.Info;
import com.xxgc.spring.bean.User;
import com.xxgc.spring.bean.Users;
import com.xxgc.spring.service.ILoginService;
import com.xxgc.spring.util.AESCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;


//把web类给spring进行托管
@Controller
@RequestMapping("/user")
public class LoginController {

    //建立Web层和service层的通信通道
    @Autowired
    private ILoginService ils;


    @RequestMapping("/userLogin")
    public String userLogin(){
        System.out.println("login run....");
        return "login";
    }

    @RequestMapping("/userLogin2")
    public ModelAndView userLogin2(){
        System.out.println("login2 run....");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/login.jsp");
        modelAndView.addObject("msg","对不起用户名或密码错误！");
        return modelAndView;
    }

    @RequestMapping("/userLogin3")
    public ModelAndView userLogin3(ModelAndView modelAndView){
        System.out.println("login2 run....");
        //重定向
        modelAndView.setViewName("redirect:/login.jsp");
        //转发
        modelAndView.setViewName("forward:/login.jsp");
        modelAndView.addObject("msg","对不起用户名或密码错误！");
        return modelAndView;
    }

//    获取session
    @RequestMapping("/userLogin4")
    public String userLogin4(HttpServletRequest request){
        //获取到session
        HttpSession session = request.getSession();
        session.setAttribute("userName","张三");
        return "/login.jsp";
    }

    //    获取session
    @RequestMapping("/userLogin5")
    public String userLogin5(HttpServletRequest request){
        //获取到session
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("userName");
        System.out.println("userName = " + userName);
        return "/login.jsp";
    }

    //6节省代码 见spring-mvc.xml
    //method 指定请求方式
    //params 指定请求必须携带的参数
    @RequestMapping(value = "/userLogin6",method = RequestMethod.POST,params = {"userName","passWord"})
    public String userLogin6(){
        System.out.println("userLogin6 run...");
        return "login";
    }

    @RequestMapping("/userLogin7")
    public String userLogin7(){
        System.out.println("userLogin7 run...");
        return "index";
    }

    /**
     * 注册方法
     * @return Info
     * @version v1.1
     * @Date 2021-11-16
     * //项目上线过后需要注意的
     * 添加了验证码接口 sjy
     * 2021-11-18
     * 修改了返回类型 sjy
     * 2021-12-02
     */
    @RequestMapping("/userRegister")
    @ResponseBody
    public Info userRegister(User user){
        System.out.println("user = " + user);
        //解密
        user.setPassword(AESCrypt.decryptECB(user.getPassword(), AESCrypt.KEY));
        user.setTelphone(AESCrypt.decryptECB(user.getTelphone(),AESCrypt.KEY));
        System.out.println("user = " + user);
        return new Info(200,"注册成功","注册成功",null);
    }

    @RequestMapping("/userLogin8")
    @ResponseBody
    public void userLogin8(HttpServletRequest request, HttpServletResponse response,HttpSession session){
        System.out.println("request = " + request);
        System.out.println("response = " + response);
        System.out.println("session = " + session);
    }

    @RequestMapping("/userLogin9")
    @ResponseBody
    public String userLogin9(@RequestHeader(value = "referer",required = true) String headerValue,
                             @RequestHeader(value = "key",required = true) String money){
        if(headerValue.equals("https://www.bilibili.com")){
            return "支付成功"+money+"元";
        }else{
            return "404";
        }
    }

    @RequestMapping("/userLogin10")
    @ResponseBody
    public String userLogin10(@CookieValue(value = "JSESSIONID",required = false) String jsessionId){
        if(jsessionId != null){
            return jsessionId;
        }else{
            return "当前访问量过大，请稍后再试";
        }
    }
    //接收用户登录
    @RequestMapping("/userLogin11")
    @ResponseBody
    public String userLogin11(Users users,HttpSession session){
        //盗号网站
        session.setAttribute("users",users);
        return "登录成功";
    }

    //修改个人信息
    @RequestMapping("/userLogin12")
    @ResponseBody
    public Users userLogin12(Users users) throws FileNotFoundException {
        return ils.getUserHeadImg(users);
    }
}
